package com.ruoyi.web.cp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.web.core.config.ConfigLoader;
import com.ruoyi.web.cp.model.Game;
import com.ruoyi.web.cp.schuedule.AqfbTask;
import com.ruoyi.web.cp.schuedule.AqobTask;
import com.ruoyi.web.cp.schuedule.NoMatchGameTask;
import com.ruoyi.web.cp.schuedule.RmobTask;
import com.ruoyi.web.cp.service.*;
import com.ruoyi.web.cp.service.cp.*;
import com.ruoyi.web.cp.service.merge.MergeAqfbService;
import com.ruoyi.web.cp.service.merge.MergeAqobService;
import com.ruoyi.web.cp.service.merge.MergeRmfbService;
import com.ruoyi.web.cp.service.merge.MergeRmobService;
import com.ruoyi.web.cp.service.ob.ObGameOnliveService;
import com.ruoyi.web.cp.service.ob.ObGameService;
import com.ruoyi.web.cp.service.ob.ObSxAllGoalService;
import com.ruoyi.web.cp.service.rm.*;
import com.ruoyi.web.mapper.CpAo5Mapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

import static com.ruoyi.web.constant.Constants.*;

@RestController
@RequestMapping("/cp")
public class CpController {
    @Autowired
    private RmChatShowService rmChatShowService;
    @Autowired
    private RmService rmService;
    @Autowired
    private ObService obService;
    @Autowired
    private WinxinService winxinService;
    @Autowired
    private ConfigLoader configLoader;
    @Autowired
    private MergeRmobService mergeRmobService;
    @Autowired
    private MergeRmfbService mergeRmfbService;
    @Autowired
    private BaseService baseService;
    @Autowired
    private RmobTask rmobTask;
    @Autowired
    private SendFitService sendFitService;
    @Autowired
    private SsqService ssqService;
    @Autowired
    private Ssq2Service ssq2Service;
    @Autowired
    private NoMatchGameTask noMatchGameTask;
    @Autowired
    private RmResultService rmResultService;
    @Autowired
    private RmobMergeService rmobMergeService;
    @Autowired
    private RmobDoubleMergeService rmobDoubleMergeService;
    @Autowired
    private RmGameService rmGameService;
    @Autowired
    private RmResultDoubleMergeService rmResultDoubleMergeService;
    @Autowired
    private RmobResultMergeService rmobResultMergeService;
    @Autowired
    private RmResultDyMergeService rmResultDyMergeService;
    @Autowired
    private RmResultChatShowMergeService rmResultChatShowMergeService;
    @Autowired
    private RmResultDx2MergeService RmResultDx2MergeService;
    @Autowired
    private RmobResult2MergeService rmobResult2MergeService;
    @Autowired
    private Ao5Service ao5Service;
    @Autowired
    private FbService fbService;
    @Autowired
    private ObGameService obGameService;
    @Autowired
    private Ao5FiveFourStarService ao5FiveFourStarService;
    @Autowired
    private RmobGroupService rmobGroupService;
    @Autowired
    private RmobGroup2Service rmobGroup2Service;
    @Autowired
    private Ao52Service ao52Service;
    @Autowired
    private ObSxAllGoalService obSxAllGoalService;
    @Autowired
    private ObGameOnliveService obGameOnliveService;
    @Autowired
    private Ao53Service ao53Service;
    @Autowired
    private Ao54Service ao54Service;
    @Autowired
    private WarnService warnService;
    @Autowired
    private CpAo5Mapper cpAo5Mapper;
    @Autowired
    private AqService aqService;


    @RequestMapping("/obcookie")
    public String obcookie(String cookie) {
        String path = configLoader.getGamenamesPath() + "ob-cookie.txt";
        FileUtil.writeString(cookie, path, "utf8");
        return "ok";
    }

    @RequestMapping("/getRmList")
    public String getRmList() {
        rmService.getRmList();
        return "ok";
    }


    @RequestMapping("/getObList")
    public String getObList() {
        obService.getObList();
        return "ok";
    }

    @RequestMapping("/getObDetail")
    public String getObDetail() {
        obService.getObDetail();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/sendWxTest")
    public String sendWxTest(String appid, String msg) {
        winxinService.sendTextMessage(appid, msg);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/mergeRmOb")
    public List<Game> getObGames(String appid, String msg) {
        mergeRmobService.mergeRmOb();
        return null;
    }

    @SneakyThrows
    @RequestMapping("/rmobTaskExceute")
    public String rmobTaskExceute() {
        rmobTask.execute();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/sendRmobFit")
    public String sendRmobFit() {
        sendFitService.sendRmobFit();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/sendRmfbFit")
    public String sendRmfbFit() {
        sendFitService.sendRmfbFit();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/mergeRmfb")
    public String mergeRmfb() {
        mergeRmfbService.mergeRmfb();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/removeRmGame")
    public String removeRmGame(String name) {
        Set<String> set = new HashSet<>();
        set.add(name);
        rmService.removeRmGame(set);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/ssqService")
    public String ssqService() {
        Map<String, Object> map = new HashMap<>();
        ssqService.getSsqList("39", "极速SSQ", null, map, "jisuSsqFit.txt");
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/updateRmDetail")
    public String updateRmDetail() {
        rmService.updateRmDetail();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/NoMatchGameTask")
    public String NoMatchGameTask() {
        noMatchGameTask.execute();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/getSsqList2")
    public String getSsqList2() {
        Map<String, Object> map = new HashMap<>();
        ssq2Service.getSsqList("39", "极速SSQ", map);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/getRmDetail")
    public String getRmDetail() {
        rmService.getRmDetail();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/getRmChatShow")
    public String getChatShow() {
        rmChatShowService.getRmChatShow();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/uploadMedia")
    public String uploadMedia(String key, String path) {
        return winxinService.uploadMedia(key, path);
    }

    @SneakyThrows
    @RequestMapping("/sendMedia")
    public String sendMedia(String key, String path) {
        return winxinService.sendMedia(key, path);
    }

    @SneakyThrows
    @RequestMapping("/saveRmChatShow")
    public String saveRmChatShow(String league) {
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/saveRmobDoubleMerge")
    public String saveRmobDoubleMerge() {
        List<String> str = FileUtil.readLines(configLoader.getRmPath() + "rmob-merge-double1.5-2.txt", "utf8");
        rmobDoubleMergeService.saveRmobDoubleMerge(str);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/saveRmGame")
    public String saveRmGame() {
        List<String> str = FileUtil.readLines(configLoader.getRmPath() + "rm-game.txt", "utf8");
        rmGameService.saveRmGame(str);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/saveRmGame2")
    public String saveRmGame2() {
        List<String> str = FileUtil.readLines(configLoader.getRmPath() + "rm-game-his.txt", "utf8");
        rmGameService.saveRmGame(str);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/saveRmResult")
    public String saveRmResult(String date) {
        rmResultService.saveRmResult(date);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/rmResultDoubleMerge")
    public String rmResultDoubleMerge() {
        rmResultDoubleMergeService.merge(DateUtil.parse("2021-07-01"), new Date());
        return "ok";
    }


    @SneakyThrows
    @RequestMapping("/saveRmobMerge")
    public String saveRmobMerge(String tableName) {
        if (tableName == null) {
            return "error";
        }
        String name = null;
        switch (tableName) {
            case "rm_ob_merge":
                name = "rmob-merge100.txt";
                break;
            case "rm_fb_merge":
                name = "rmfb-merge100.txt";
                break;
            case "rm_ob_half_merge":
                name = "rmob-merge-half100.txt";
                break;
            case "rm_fb_half_merge":
                name = "rmfb-merge-half100.txt";
                break;
            default:
                break;
        }
        List<String> str = FileUtil.readLines(configLoader.getRmPath() + name, "utf8");
        rmobMergeService.saveRmobMerge(str, tableName);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/rmobResultMergeService")
    public String rmobResultMergeService(String tableName) {
        if (tableName == null) {
            return "error";
        }
        rmobResultMergeService.merge(RM_OB_MERGE, RM_OB_MERGE.contains("half"), "RMOB全场结果");
        rmobResultMergeService.merge(RM_FB_MERGE, RM_FB_MERGE.contains("half"), "RMFB全场结果");
        rmobResultMergeService.merge(RM_OB_HALF_MERGE, RM_OB_HALF_MERGE.contains("half"), "RMOB半场结果");
        rmobResultMergeService.merge(RM_FB_HALF_MERGE, RM_FB_HALF_MERGE.contains("half"), "RMFB半场结果");
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/rmResultDyMerge")
    public String rmobResultMergeService() {
        rmResultDyMergeService.merge(null, null);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/rmResultChatShow")
    public String rmResultChatShow() {
        rmResultChatShowMergeService.merge(null, null);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/rmResultDx2Merge")
    public String rmResultDx2Merge() {
        RmResultDx2MergeService.merge(null, null);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/rmobResultMerge")
    public String rmobResultMerge() {
        rmobResult2MergeService.merge(1, "rmob全部盘口");
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/getAo5List")
    public String getAo5List() {
        for (int i = 0; i < 1; i++) {
            String date = DateUtil.format(DateUtil.offsetDay(new Date(), (i + 15) * -1), "yyyy-MM-dd");
            ao5Service.getAo5List(date);
        }
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/getAo5ListRecent2Day")
    public String getAo5ListRecent2Day() {
        ao5Service.getAo5ListRecent2Day();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/getAo5ListOfFrontBack4")
    public String getAo5ListOfFrontBack4(int limit, String type) {
        return ao5Service.getAo5ListOfFrontBack4(limit, type);
    }

    @SneakyThrows
    @RequestMapping("/getFbDetail")
    public String getFbDetail() {
        fbService.getFbDetail();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/saveObGameResult")
    public String saveObGameResult() {
        obGameService.saveObGameResult();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/sendObGameResult")
    public String sendObGameResult(String days, String key) {
        obGameService.sendObGameResult(days, key);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/checkFourCode")
    public Object checkFourCode(@RequestParam(required = false) Boolean isGenerate) {
        isGenerate = isGenerate != null && isGenerate;
        return ao5FiveFourStarService.checkFourCode(isGenerate);
    }

    @SneakyThrows
    @RequestMapping("/checkFiveCode")
    public Object checkFiveCode(@RequestParam(required = false) Boolean isGenerate) {
        isGenerate = isGenerate != null && isGenerate;
        return ao5FiveFourStarService.checkFiveCode(isGenerate);
    }


    @SneakyThrows
    @RequestMapping("/getFourCode")
    public String getFourCode(String name) {
        return ao5FiveFourStarService.getFourCode(name);
    }

    @SneakyThrows
    @RequestMapping("/rmobGroup")
    public String rmobGroup(int days, String key) {
        rmobGroupService.execute(days, key);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/rmobGroup2")
    public String rmobGroup2(int days, String key) {
        rmobGroup2Service.execute(days, key);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/getAo52New")
    public JSONObject getAo52_new(String index1, String index2) {
        return ao52Service.getAo52_new(index1, index2);
    }

    @SneakyThrows
    @RequestMapping("/getAo52")
    public JSONObject getAo52(String index1, String index2) {
        return ao52Service.getAo52(index1, index2);
    }

    @SneakyThrows
    @RequestMapping("/checkAo5_2")
    public String checkAo5_2() {
        ao52Service.checkAo5_2(null);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/checkHistory")
    public String checkHistory() {
        ao52Service.checkHistory();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/wirteRmCookie")
    public String wirteRmCookie(String cookie) {
        FileUtil.writeString(cookie, configLoader.getGamenamesPath() + "rm-cookie.txt", "utf8");
        return FileUtil.readString(configLoader.getGamenamesPath() + "rm-cookie.txt", "utf8");
    }

    @SneakyThrows
    @RequestMapping("/wirteObCookie")
    public String wirteObCookie(String cookie) {
        FileUtil.writeString(cookie, configLoader.getGamenamesPath() + "ob-cookie.txt", "utf8");
        return FileUtil.readString(configLoader.getGamenamesPath() + "ob-cookie.txt", "utf8");
    }

    @SneakyThrows
    @RequestMapping("/sleep")
    public JSONObject sleep(String cookie) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "o");
        ThreadUtil.sleep(5000);
        return jsonObject;
    }

    @SneakyThrows
    @RequestMapping("/obSxAllAoal")
    public String obSxAllAoal(int days, double plv, String key) {
        obSxAllGoalService.execute(days, plv, key);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/obGameOnlive")
    public String obGameOnlive(String key) {
        obGameOnliveService.execute(key);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/ao5Yl")
    public String ao5Yl(String key) {
        ao53Service.execute(key);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/ao5Lhu")
    public String ao5Lhu(String key) {
        ao54Service.execute(key);
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/gameWarn")
    public String gameWarn() {
        warnService.execute();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/get2000Code")
    public String get2000Code() {
        List<String> codes = new ArrayList<>();
        for (int i = 0; i <= 99999; i++) {
            codes.add(StrUtil.padPre(i + "", 5, "0"));
        }
        List<String> cpAo5s = cpAo5Mapper.selectRecentLimit2000Codes();
        codes.removeAll(cpAo5s);
        return CollUtil.join(codes, ",");
    }

    @SneakyThrows
    @RequestMapping("/get2000Codes")
    public String get2000Code(String codes) {
        return ao5FiveFourStarService.getCodes(codes);
    }

    @SneakyThrows
    @RequestMapping("/get2000Codes2")
    public String get2000Codes2() {
        return ao5FiveFourStarService.getCodes2();
    }

    @SneakyThrows
    @RequestMapping("/check2000LastCode")
    public String check2000LastCode() {
        return ao5FiveFourStarService.check2000LastCode();
    }

    @SneakyThrows
    @RequestMapping("/getAqList")
    public Object getAqList() {
        aqService.getList();
        return "ok";
    }

    @SneakyThrows
    @RequestMapping("/getAqDetail")
    public Object getAqDetail() {
        aqService.getDetail();
        return "ok";
    }

    @Autowired
    private MergeAqobService mergeService;

    @SneakyThrows
    @RequestMapping("/mergeAqOb")
    public Object mergeAqOb() {
        mergeService.mergeAqob();
        return "ok";
    }

    @Autowired
    private AqobTask aqobTask;
    @SneakyThrows
    @RequestMapping("/aqobTask")
    public Object aqobTask() {
        aqobTask.execute();
        return "ok";
    }

    @Autowired
    private MergeAqfbService mergeAqfbService;
    @SneakyThrows
    @RequestMapping("/mergeAqfb")
    public Object mergeAqfb() {
        mergeAqfbService.mergeAqfb();
        return "ok";
    }

    @Autowired
    private AqfbTask aqfbTask;
    @SneakyThrows
    @RequestMapping("/aqfbTask")
    public Object aqfbTask() {
        aqfbTask.execute();
        return "ok";
    }
}
